package com.sgb.kjwl.view.ui.adapter.self.publish;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.DeviceStateEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class DeviceStateAdapter extends BaseRecycleAdapter<DeviceStateEntity> {
    private OptCallBack optCallBack;
    private TextView delView;
    private TextView editView;
    private TextView downView;
    private TextView upView;
    private View lineView;
    private AlertDialog.Builder builder;
    private AlertDialog dialog;

    public DeviceStateAdapter(Context context, List<DeviceStateEntity> datas) {
        super(context, R.layout.item_device_state, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final DeviceStateEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        delView = holder.getItem().findViewById(R.id.del_view);
        editView = holder.getItem().findViewById(R.id.edit_view);
        downView = holder.getItem().findViewById(R.id.down_view);
        upView = holder.getItem().findViewById(R.id.up_view);
        lineView = holder.getItem().findViewById(R.id.line_view);

        if (entity.getState().equals("0")) {
            downView.setVisibility(View.GONE);
            upView.setVisibility(View.GONE);
            editView.setVisibility(View.VISIBLE);
            delView.setVisibility(View.VISIBLE);
        } else if (entity.getState().equals("1")) {
            downView.setVisibility(View.VISIBLE);
            upView.setVisibility(View.GONE);
            editView.setVisibility(View.GONE);
            delView.setVisibility(View.GONE);
        } else if (entity.getState().equals("2")) {
            upView.setVisibility(View.VISIBLE);
            downView.setVisibility(View.GONE);
            editView.setVisibility(View.VISIBLE);
            delView.setVisibility(View.VISIBLE);
        }else if(entity.getState().equals("3")){
            upView.setVisibility(View.GONE);
            downView.setVisibility(View.GONE);
            editView.setVisibility(View.VISIBLE);
            delView.setVisibility(View.VISIBLE);
        }else if(entity.getState().equals("4")){
            upView.setVisibility(View.GONE);
            downView.setVisibility(View.GONE);
            editView.setVisibility(View.GONE);
            delView.setVisibility(View.GONE);
            lineView.setVisibility(View.GONE);
        }

        delView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(R.id.del_view, entity, "删除提示", "您确定要删除当前信息？ 删除后，将不可恢复");
            }
        });
        editView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (optCallBack != null) {
                    optCallBack.edit(entity);
                }
            }
        });
        downView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(R.id.off_view, entity, "下架提示", "您确定要下架当前信息？");
            }
        });
        upView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(R.id.online_view, entity, "上架提示", "您确定要上架当前信息？");
            }
        });
    }

    public interface OptCallBack {
        void del(String id);

        void onlinOff(String id, String state);

        void edit(DeviceStateEntity entity);
    }

    public void setOptCallBack(OptCallBack optCallBack) {
        this.optCallBack = optCallBack;
    }

    public void showDialog(final int position, final DeviceStateEntity entity, String title, String content) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.dialog_layout, null);
        builder = new AlertDialog.Builder(context);
        builder.setView(layout);
        builder.setCancelable(true);
        dialog = builder.show();
        TextView cancel_tv = layout.findViewById(R.id.cancel_tv);
        TextView ok_tv = layout.findViewById(R.id.ok_tv);
        TextView title_tv = layout.findViewById(R.id.title_tv);
        TextView content_tv = layout.findViewById(R.id.content_tv);
        title_tv.setText(title);
        content_tv.setText(content);
        ok_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (position) {
                    case R.id.del_view:
                        if (optCallBack != null) {
                            optCallBack.del(entity.getId());
                        }
                        break;
                    case R.id.off_view:
                        if (optCallBack != null) {
                            optCallBack.onlinOff(entity.getId(), "2");
                        }
                        break;
                    case R.id.online_view:
                        if (optCallBack != null) {
                            optCallBack.onlinOff(entity.getId(), "1");
                        }
                        break;
                }
                dialog.dismiss();
            }
        });
        cancel_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
}
